package com.atf.atfconfigurator.views;

import org.apache.commons.configuration.PropertiesConfiguration;

import com.atf.atfconfigurator.AtfconfiguratorUI;
import com.atf.atfconfigurator.composite.CollapseableBox;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

public class J2CAlias extends VerticalLayout implements View {
	private PropertiesConfiguration wasConfigProperty;
	@Override
	public void enter(ViewChangeEvent event) {
		// TODO Auto-generated method stub
		
		wasConfigProperty=((AtfconfiguratorUI) getUI()).getWasConfiguration();	
		setSpacing(true);
        setMargin(true);

        Label title = new Label(wasConfigProperty.getString("appName"));
        title.addStyleName("h2");
        title.addStyleName("colored");
        addComponent(title);
        
		final FormLayout form = new FormLayout();
        form.setMargin(false);
        form.setSpacing(true);
        form.setWidth("100%");
        //form.set
        form.addStyleName("light");
        addComponent(form);
        
        
        final TextField numberOfAlias = new TextField("Number of J2C Alias");
	    int numberOfAliasValue=wasConfigProperty.getInt("numberOfAlias",0);
	    numberOfAlias.setValue(String.valueOf(numberOfAliasValue));
	    numberOfAlias.setWidth("50%");
	    form.addComponent(numberOfAlias);
	    
	    numberOfAlias.addValueChangeListener(new Property.ValueChangeListener(){

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void valueChange(ValueChangeEvent event) {
				// TODO Auto-generated method stub
				((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("numberOfAlias",  (String) event.getProperty().getValue());
				((AtfconfiguratorUI) getUI()).navigator.navigateTo("J2CAlias");
			}        	
        });
	     if(numberOfAliasValue > 0){
	    	 for(int i=1; i<numberOfAliasValue+1; i++){
	    		 form.addComponent(new CollapseableBox(String.valueOf(i),getJ2CAliasEntries(i), false));
	    	 }
	     }
	}
	private Component getJ2CAliasEntries(final int pos) {
		// TODO Auto-generated method stub
		final FormLayout form = new FormLayout();
		//application Name
		form.setMargin(false);
        form.setSpacing(true);
        form.setWidth("100%");
        //form.set
        form.addStyleName("light");
        String name=wasConfigProperty.getString("Alias"+pos+"Name", "");
        String username=wasConfigProperty.getString("Alias"+pos+"Username", "");
        String password=wasConfigProperty.getString("Alias"+pos+"Password", "");
        
        
        //Name
        final TextField nameText = new TextField("Name");
        nameText.setValue(name);
        nameText.setWidth("100%");
        nameText.addValueChangeListener(new Property.ValueChangeListener(){

   			@Override
   			public void valueChange(ValueChangeEvent event) {
   				// TODO Auto-generated method stub
   				String value = (String) event.getProperty().getValue();
   				((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("Alias"+pos+"Name", value);
   			}
           	
           });
        nameText.setImmediate(true);
        form.addComponent(nameText);
        
        //Username 
        final TextField usernameText = new TextField("Username");
        usernameText.setValue(name);
        usernameText.setWidth("100%");
        usernameText.addValueChangeListener(new Property.ValueChangeListener(){

   			@Override
   			public void valueChange(ValueChangeEvent event) {
   				// TODO Auto-generated method stub
   				String value = (String) event.getProperty().getValue();
   				((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("Alias"+pos+"Username", value);
   			}
           	
           });
        usernameText.setImmediate(true);
        form.addComponent(usernameText);
        
        
        //Password
        final PasswordField passwordText = new PasswordField("Password");
        passwordText.setValue(name);
        passwordText.setWidth("100%");
        passwordText.addValueChangeListener(new Property.ValueChangeListener(){

   			@Override
   			public void valueChange(ValueChangeEvent event) {
   				// TODO Auto-generated method stub
   				String value = (String) event.getProperty().getValue();
   				((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("Alias"+pos+"Password", value);
   			}
           	
           });
        passwordText.setImmediate(true);
        form.addComponent(passwordText);
        
        
        return form;
	}

}
